﻿using Sidvall.UI.Dialogs;
using Sidvall.UI.ViewModels;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace Sidvall.UI.Wpf.Controls
{
    public partial class ProgressControl : UserControl
    {
        #region Public Members

        public event System.EventHandler CloseSelected;

        #region ViewModel

        public ProgressControlViewModel ViewModel
        {
            get
            {
                return this.DataContext as ProgressControlViewModel;
            }
            set
            {
                var oldValue = this.DataContext as ProgressControlViewModel;
                if (oldValue != null)
                    oldValue.CloseSelected -= OnCloseSelected;
                this.DataContext = value;
                if (value != null)
                    value.CloseSelected += OnCloseSelected;
            }
        }

        #endregion

        #region InitializeContext

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
        public void InitializeContext(ProgressDialogContext context)
        {
            if (context == null)
                throw new System.ArgumentNullException("context");

            var item = new ProgressControlViewModel();
            item.Initialize(context);
            this.Context = context;
            this.ViewModel = item;
        }

        #endregion
        #region StartAsync

        public async Task StartAsync()
        {
            if (this.ViewModel.MonitoredTaskItem.Status == Sidvall.Tasks.TaskStatus.Created)
            {
                await this.ViewModel.MonitoredTaskItem.ExecuteAsync(this.Context.ExecuteTasksAsOne);
                if (this.ViewModel != null)
                {
                    switch (this.ViewModel.MonitoredTaskItem.Status)
                    {
                        case Tasks.TaskStatus.Faulted:
                            if (this.ViewModel.CloseWhenFaulted)
                                OnCloseSelected();
                            break;
                        default:
                            if (this.ViewModel.CloseWhenFinished)
                                OnCloseSelected();
                            break;
                    }
                }
            }
        }

        #endregion
        #region Stop

        public void Stop()
        {
            if (this.ViewModel != null)
            {
                this.ViewModel.MonitoredTaskItem.Cancel();
                this.Context.Status = this.ViewModel.MonitoredTaskItem.Status;
                this.Context.Exception = this.ViewModel.MonitoredTaskItem.Exception;
                this.ViewModel.Dispose();
                this.ViewModel = null;
            }
        }

        #endregion

        #endregion
        #region Private Members

        private ProgressDialogContext Context { get; set; }

        #region OnCloseSelected

        private void OnCloseSelected()
        {
            OnCloseSelected(new System.EventArgs());
        }
        private void OnCloseSelected(System.EventArgs args)
        {
            OnCloseSelected(this, args);
        }
        private void OnCloseSelected(object sender, System.EventArgs args)
        {
            if (this.CloseSelected != null)
                this.CloseSelected.Invoke(sender, args);
        }

        #endregion

        #endregion
        #region Constructors

        public ProgressControl()
        {
            InitializeComponent();
        }

        #endregion
    }
}
